package com.chenshang.controller;

import java.lang.reflect.Method;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.chenshang.config.ConfigLoader;
import com.chenshang.config.Environment;
import com.chenshang.config.InvokeInfo;
import com.chenshang.util.JaxbUtil;
import com.travelzen.flight.base.RequestEntity;
import com.travelzen.flight.base.TzRequest;

@Controller
public class InvokCtrl {

	public static final Logger LOGGER = (Logger) LoggerFactory.getLogger(InvokCtrl.class);

	@RequestMapping(value = { "/invok" }, method = { RequestMethod.POST })
	public static ModelAndView invok(String request, @RequestParam(required = false) String enviorment, Map<String, Object> result) {
		long start = System.currentTimeMillis();
		TzRequest tzRequest = JaxbUtil.convertToTzRequestByXml(request);
		Class<? extends RequestEntity> tzRquestClass = tzRequest.getRequestEntity().getClass();
		String serviceName = tzRquestClass.getName() + "Service";
		Map<String, InvokeInfo> invokeInfos = ConfigLoader.getInstance().getInvokeInfos();
		InvokeInfo info = invokeInfos.get(serviceName);
		String className = info.className;
		String methodName = info.method;

		String tzResponse = "";
		try {
			Class<?> service = Class.forName(className);
			Method method = service.getDeclaredMethod(methodName, Environment.class, Boolean.class, TzRequest.class, Map.class);
			tzResponse = (String) method.invoke(null, Environment.op3, false, tzRequest, result);
		} catch (Exception e) {
			e.printStackTrace();
		}

		
		
		
		
		
		System.out.println("用时：" + (System.currentTimeMillis() - start));
		result.put("request", request);
		return new ModelAndView("index");
	}
	
	
}
